<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars
define("PRIMARY_KEY","noref");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_transaction_advreport_opr.php";
$FileTemplate  = "ais_transaction_advreport_opr_list.html";
$TableName     = "ais_advance";
$SubHeaderTitle= "Transaction Advance - Report Advance";
$PageSize      = 10 ;
$HTMLGridList  = "AdvanceGrid";
//************ FORM ACTION **************
    if (GetParam("Action","")=="Delete") {
        DeleteRecord ("txtCheck",$PageSize);
    }

//Initiate Database Table For Search;
$FCode = GetParam("advCodeSearch","");
//$FSkada = GetParam("skadaSearch","");
//$FDept = GetParam("deptSearch","");
$QueryGet  = GetQueryString("All", array('PageNum'));


//$FLedger = (strlen(GetParam("ledger",""))>0)?GetParam("ledger",""):$DBConnection->dbc->get_var($SQLLedger);
$FLedger = GetParam("ledger","");


//******* DATABASE FORM QUERY ***********

/*
$SQL = "
            SELECT *, sum(jumlah) as sumjumlah
            FROM `$TableName`
            LEFT JOIN
            ais_history_trans ON $TableName.nobkt=ais_history_trans.entry1 AND $TableName.ktrasl=ais_history_trans.ktrasl

          ";
*/

$SQL = "
            SELECT $TableName.*, sum(b.total_amount) as sumjumlah2, sum($TableName.jumlah) as sumjumlah, ais_memorial.nobkt as entry3, ais_memorial.jumlah as amount3,
            IF (L.nobkt<>'',L.nobkt,IF(LZ.nobkt<>'',LZ.nobkt,'')) as entry4,
            IF (L.jumlah<>'',L.jumlah,IF(LZ.jumlah<>'',LZ.jumlah,'')) as amount4,
            bb_bk.jumlah as amount2, bb_bk.nobkt as aslref
            FROM `$TableName`
            INNER JOIN
            ais_memorial bb_bk ON $TableName.nobkt=bb_bk.noref AND bb_bk.ktrasl=$TableName.ktrasl AND bb_bk.urut=999
            LEFT JOIN
            ais_memorial ON bb_bk.nobkt=ais_memorial.noref AND ais_memorial.ktrasl=$TableName.ktrasl AND ais_memorial.urut=999 AND ais_memorial.nobkt like 'M%'
            LEFT JOIN
            ais_memorial L ON L.noref=bb_bk.nobkt AND L.ktrasl=bb_bk.ktrasl AND L.urut=999 AND L.nobkt not like 'M%'
            LEFT JOIN
            ais_memorial_z LZ ON LZ.noref=bb_bk.nobkt AND LZ.ktrasl=bb_bk.ktrasl AND LZ.urut=999  AND LZ.nobkt not like 'M%'
            LEFT JOIN
            tbljoborder_costing b ON $TableName.jo_costing_id = b.jo_costing_id
            LEFT JOIN
            tbljoborder c ON b.jo_id = c.jo_id ";


$WHERE   = "$TableName.ktrasl='".GetOfficeID()."' ";

$WHERE  .=  " AND ifnull(bb_bk.nobkt,'')<>'' ";
$WHERE  .=  " AND ifnull(bb_bk.jumlah,0)-ifnull(ais_memorial.jumlah,0) <> 0 ";
$WHERE  .=  " AND IF (L.nobkt<>'',L.nobkt,IF(LZ.nobkt<>'',LZ.nobkt,''))='' ";

//Retrieve Parameter from search
$SQLLedger = "SELECT ledger FROM ais_history_ledger ORDER BY ledger DESC LIMIT 1";

/*
if ($FLedger) {
    $CustomWhere = " $TableName.ledger like '".$FLedger."'";
    if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
    else $WHERE = $CustomWhere;
}
* */

if ($FCode) {
    $CustomWhere = " $TableName.nobkt like '".$FCode."%'";
    if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
    else $WHERE = $CustomWhere;
}

if (strlen($WHERE)) {
    $SQL = $SQL."WHERE ".$WHERE;
    $SQLCount = $SQLCount."WHERE ".$WHERE;
}

$SQL .= " GROUP BY $TableName.nobkt ORDER BY ".$TableName.".adv_id DESC";
$SQLCount  .= " GROUP BY $TableName.nobkt";





//SQL Count

$SQLCount = "
            SELECT count(*)
            FROM `$TableName`
            INNER JOIN
            ais_memorial bb_bk ON $TableName.nobkt=bb_bk.noref AND bb_bk.ktrasl=$TableName.ktrasl AND bb_bk.urut=999
            LEFT JOIN
            ais_memorial ON bb_bk.nobkt=ais_memorial.noref AND ais_memorial.ktrasl=$TableName.ktrasl AND ais_memorial.urut=999 AND ais_memorial.nobkt like 'M%'
            LEFT JOIN
            ais_memorial L ON L.noref=bb_bk.nobkt AND L.ktrasl=bb_bk.ktrasl AND L.urut=999 AND L.nobkt not like 'M%'
            LEFT JOIN
            ais_memorial_z LZ ON LZ.noref=bb_bk.nobkt AND LZ.ktrasl=bb_bk.ktrasl AND LZ.urut=999  AND LZ.nobkt not like 'M%'
            LEFT JOIN
            tbljoborder_costing b ON $TableName.jo_costing_id = b.jo_costing_id
            LEFT JOIN
            tbljoborder c ON b.jo_id = c.jo_id ";


$WHERE   = "$TableName.ktrasl='".GetOfficeID()."' ";

$WHERE  .=  " AND ifnull(bb_bk.nobkt,'')<>'' ";
$WHERE  .=  " AND ifnull(bb_bk.jumlah,0)-ifnull(ais_memorial.jumlah,0) <> 0 ";
$WHERE  .=  " AND IF (L.nobkt<>'',L.nobkt,IF(LZ.nobkt<>'',LZ.nobkt,''))='' ";


if ($FCode) {
    $CustomWhere = " $TableName.nobkt like '".$FCode."%'";
    if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
    else $WHERE = $CustomWhere;
}

if (strlen($WHERE)) {
    $SQLCount = $SQLCount."WHERE ".$WHERE;
}

$SQLCount  .= " GROUP BY $TableName.nobkt";

//SQL Count



//print $SQL;
//Select Customer
//$SQLDept = "SELECT * FROM tbldept";

//******* SHOW FORM CONTENT *********
$PageNum = 1;
/*
$RecCnt = $DBConnection->dbc->get_var($SQLCount) ;
if (!strlen($RecCnt)) $RecCnt = 0;
*/
if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

//Default value
if (!isset($_GET)) $_GET=&$HTTP_GET_VARS ;
if (isset($_GET['PageNum'])) {
  $PageNum = $_GET['PageNum'] ;
} else {
    $PageNum = 1 ;
}

//Default value
if (isset($_GET['RecCnt'])) {
  $RecCnt = intval($_GET['RecCnt']) ;
} else {
    $RecCnt = -1 ;
}

/*$rama = mysql_query($SQL);
$kozhie = mysql_fetch_array($result)

*/
//echo "$SQL"; exit;

$TBS = new clsTinyButStrong ;
$SQLLedger = "SELECT * FROM ais_history_ledger ORDER BY ledger DESC ";
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
$RecCnt = $TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
//$TBS->MergeBlock("deptblk",$DBMysql,$SQLDept);
$TBS->MergeBlock("ledgerblk",$DBMysql,$SQLLedger);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ;
$TBS->Show();


function DeleteRecord ($sequence_name,$PageSize) {
    global $DBConnection,$FileName,$TableName;
    for ($x=1;$x<=$PageSize;$x++) {
        if (GetParam($sequence_name.$x,"")) {
            $DBConnection->Delete("$TableName",$sequence_name.$x);
        }
    }
    header("Location: $FileName");
    exit;

}

function m_event_blg ($BlockName,&$CurrRec,&$DetailSrc,$RecNum) {
    global $FileName;
        $remain = $CurrRec["amount3"] - $CurrRec["amount2"];
        if ($remain<0) $remain=$remain*-1;
        if (strlen($CurrRec["aslref"]) && ($CurrRec["entry3"]==""))  {
           //$CurrRec["entry3"] = "<a href=$FileName?noref=".$CurrRec["aslref"]."&PageMode=FormMM>MM</a>";
           $CurrRec["entry3"] = "<a href=$FileName?noref=".$CurrRec["aslref"]."&refBS=".$CurrRec["nobkt"]."&PageMode=FormMM>MB</a>";
        }

    if ($CurrRec["amount3"] > $CurrRec["amount2"]) { //bb/bk
        if (!strlen($CurrRec["entry4"])) {
            $CurrRec["entry4"] = "<a href=$FileName?PageMode=FormBB&noref=".$CurrRec["aslref"]."&remain=$remain>BB</a>/<a href=$FileName?PageMode=FormBK&noref=".$CurrRec["aslref"]."&remain=$remain>BK</a>";
        }
    } elseif ($CurrRec["amount3"] < $CurrRec["amount2"]) { //tb/tk
        if ((!strlen($CurrRec["entry4"])) && ($CurrRec["amount3"]>=0) && $CurrRec["amount3"]<>'') {
            $CurrRec["entry4"] = "<a href=$FileName?PageMode=FormTB&noref=".$CurrRec["aslref"]."&remain=$remain>TB</a>/<a href=$FileName?PageMode=FormTK&noref=".$CurrRec["aslref"]."&remain=$remain>TK</a>";
        }
    }
}

?>
